home *** CD-ROM | disk | FTP | other *** search
/ STraTOS 1997 April & May / STraTOS 1 - 1997 April & May.iso / CD01 / INTERNET / SITES / LITTLE / P3SRC.ZIP / ATARI / VECTOR.H < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-18  |  7.5 KB  |  206 lines

  1. /****************************************************************************
  2. *                vector.h
  3. *
  4. *  This module contains macros to perform operations on vectors.
  5. *
  6. *  from Persistence of Vision(tm) Ray Tracer
  7. *  Copyright 1996 Persistence of Vision Team
  8. *---------------------------------------------------------------------------
  9. *  NOTICE: This source code file is provided so that users may experiment
  10. *  with enhancements to POV-Ray and to port the software to platforms other 
  11. *  than those supported by the POV-Ray Team.  There are strict rules under
  12. *  which you are permitted to use this file.  The rules are in the file
  13. *  named POVLEGAL.DOC which should be distributed with this file. If 
  14. *  POVLEGAL.DOC is not available or for more info please contact the POV-Ray
  15. *  Team Coordinator by leaving a message in CompuServe's Graphics Developer's
  16. *  Forum.  The latest version of POV-Ray may be found there as well.
  17. *
  18. * This program is based on the popular DKB raytracer version 2.12.
  19. * DKBTrace was originally written by David K. Buck.
  20. * DKBTrace Ver 2.0-2.12 were written by David K. Buck & Aaron A. Collins.
  21. *
  22. *****************************************************************************/
  23.  
  24. #ifndef VECTOR_H
  25. #define VECTOR_H
  26.  
  27.  
  28.  
  29.  
  30. /* Misc. Vector Math Macro Definitions */
  31.  
  32. extern DBL VTemp;
  33.  
  34. /* Vector Add */
  35. #define VAdd(a, b, c) {(a)[X]=(b)[X]+(c)[X];(a)[Y]=(b)[Y]+(c)[Y];(a)[Z]=(b)[Z]+(c)[Z];}
  36. #define VAddEq(a, b) {(a)[X]+=(b)[X];(a)[Y]+=(b)[Y];(a)[Z]+=(b)[Z];}
  37.  
  38. /* Vector Subtract */
  39. #define VSub(a, b, c) {(a)[X]=(b)[X]-(c)[X];(a)[Y]=(b)[Y]-(c)[Y];(a)[Z]=(b)[Z]-(c)[Z];}
  40. #define VSubEq(a, b) {(a)[X]-=(b)[X];(a)[Y]-=(b)[Y];(a)[Z]-=(b)[Z];}
  41.  
  42. /* Scale - Multiply Vector by a Scalar */
  43. #define VScale(a, b, k) {(a)[X]=(b)[X]*(k);(a)[Y]=(b)[Y]*(k);(a)[Z]=(b)[Z]*(k);}
  44. #define VScaleEq(a, k) {(a)[X]*=(k);(a)[Y]*=(k);(a)[Z]*=(k);}
  45.  
  46. /* Inverse Scale - Divide Vector by a Scalar */
  47.  
  48. #define VInverseScale(a, b, k) {(a)[X]=(b)[X]/(k);(a)[Y]=(b)[Y]/(k);(a)[Z]=(b)[Z]/(k);}
  49. #define VInverseScaleEq(a, k) {(a)[X]/=(k);(a)[Y]/=(k);(a)[Z]/=(k);}
  50. /*
  51. #define VInverseScale(a, b, k) {(a)[X]=(b)[X]*(1/k);(a)[Y]=(b)[Y]*(1/k);(a)[Z]=(b)[Z]*(1/k);}
  52. #define VInverseScaleEq(a, k) {(a)[X]*=(1/k);(a)[Y]*=(1/k);(a)[Z]*=(1/k);}
  53. */
  54.  
  55. /* Dot Product - Gives Scalar angle (a) between two vectors (b) and (c) */
  56. #define VDot(a, b, c) {a=(b)[X]*(c)[X]+(b)[Y]*(c)[Y]+(b)[Z]*(c)[Z];}
  57.  
  58. /* Cross Product - returns Vector (a) = (b) x (c) 
  59.    WARNING:  a must be different from b and c.*/
  60. #define VCross(a,b,c) {(a)[X]=(b)[Y]*(c)[Z]-(b)[Z]*(c)[Y]; \
  61.                        (a)[Y]=(b)[Z]*(c)[X]-(b)[X]*(c)[Z]; \
  62.                        (a)[Z]=(b)[X]*(c)[Y]-(b)[Y]*(c)[X];}
  63.  
  64. /* Evaluate - returns Vector (a) = Multiply Vector (b) by Vector (c) */
  65. #define VEvaluate(a, b, c) {(a)[X]=(b)[X]*(c)[X];(a)[Y]=(b)[Y]*(c)[Y];(a)[Z]=(b)[Z]*(c)[Z];}
  66. #define VEvaluateEq(a, b) {(a)[X]*=(b)[X];(a)[Y]*=(b)[Y];(a)[Z]*=(b)[Z];}
  67.  
  68. /* Divide - returns Vector (a) = Divide Vector (b) by Vector (c) */
  69. #define VDiv(a, b, c) {(a)[X]=(b)[X]/(c)[X];(a)[Y]=(b)[Y]/(c)[Y];(a)[Z]=(b)[Z]/(c)[Z];}
  70. #define VDivEq(a, b) {(a)[X]/=(b)[X];(a)[Y]/=(b)[Y];(a)[Z]/=(b)[Z];}
  71.  
  72. /* Simple Scalar Square Macro */
  73. #define Sqr(a)  ((a)*(a))
  74.  
  75. /* Square a Vector (b) and Assign to another Vector (a) */
  76. #define VSquareTerms(a, b) {(a)[X]=(b)[X]*(b)[X];(a)[Y]=(b)[Y]*(b)[Y];(a)[Z]=(b)[Z]*(b)[Z];}
  77.  
  78. /* Vector Length - returs Scalar Euclidean Length (a) of Vector (b) */
  79. #define VLength(a, b) {a=sqrt((b)[X]*(b)[X]+(b)[Y]*(b)[Y]+(b)[Z]*(b)[Z]);}
  80.  
  81. /* Vector Distance - returs Scalar Euclidean Distance (a) between two
  82.  * points/Vectors (b) and (c) */
  83. #define VDist(a, b, c) {VECTOR tmp; VSub(tmp, b, c); VLength(a, tmp);}
  84.  
  85. /* Normalize a Vector - returns a vector (length of 1) that points at (b) */
  86. #define VNormalize(a,b) {VTemp=1./sqrt((b)[X]*(b)[X]+(b)[Y]*(b)[Y]+(b)[Z]*(b)[Z]);(a)[X]=(b)[X]*VTemp;(a)[Y]=(b)[Y]*VTemp;(a)[Z]=(b)[Z]*VTemp;}
  87. #define VNormalizeEq(a) {VTemp=1./sqrt((a)[X]*(a)[X]+(a)[Y]*(a)[Y]+(a)[Z]*(a)[Z]);(a)[X]*=VTemp;(a)[Y]*=VTemp;(a)[Z]*=VTemp;}
  88.  
  89. /* Compute a Vector (a) Halfway Between Two Given Vectors (b) and (c) */
  90. #define VHalf(a, b, c) {(a)[X]=0.5*((b)[X]+(c)[X]);(a)[Y]=0.5*((b)[Y]+(c)[Y]);(a)[Z]=0.5*((b)[Z]+(c)[Z]);}
  91.  
  92. /* Calculate the sum of the sqares of the components of a vector.  (the square of its length) */
  93. #define VSumSqr(a)  ((a)[X]*(a)[X] + (a)[Y]*(a)[Y] + (a)[Z]*(a)[Z])
  94.  
  95.  
  96.  
  97. /*
  98.  * Linear combination of 2 vectors. [DB 7/94]
  99.  *
  100.  *   V = k1 * V1 + k2 * V2
  101.  */
  102. #define VLinComb2(V, k1, V1, k2, V2)            \
  103.   { (V)[X] = (k1) * (V1)[X] + (k2) * (V2)[X];   \
  104.     (V)[Y] = (k1) * (V1)[Y] + (k2) * (V2)[Y];   \
  105.     (V)[Z] = (k1) * (V1)[Z] + (k2) * (V2)[Z]; }
  106.  
  107.  
  108.  
  109. /*
  110.  * Linear combination of 3 vectors. [DB 7/94]
  111.  *
  112.  *   V = k1 * V1 + k2 * V2 + k3 * V3
  113.  */
  114. #define VLinComb3(V, k1, V1, k2, V2, k3, V3)                     \
  115.   { (V)[X] = (k1) * (V1)[X] + (k2) * (V2)[X] + (k3) * (V3)[X];   \
  116.     (V)[Y] = (k1) * (V1)[Y] + (k2) * (V2)[Y] + (k3) * (V3)[Y];   \
  117.     (V)[Z] = (k1) * (V1)[Z] + (k2) * (V2)[Z] + (k3) * (V3)[Z]; }
  118.  
  119.  
  120.  
  121. /*
  122.  * Evaluate a ray equation. [DB 7/94]
  123.  *
  124.  *   IPoint = Initial + depth * Direction
  125.  */
  126. #define VEvaluateRay(IPoint, Initial, depth, Direction)      \
  127.   { (IPoint)[X] = (Initial)[X] + (depth) * (Direction)[X];   \
  128.     (IPoint)[Y] = (Initial)[Y] + (depth) * (Direction)[Y];   \
  129.     (IPoint)[Z] = (Initial)[Z] + (depth) * (Direction)[Z]; }
  130.  
  131.  
  132.  
  133. /*
  134.  * Add a scaled vector. [DB 7/94]
  135.  *
  136.  *   V  = V1 + k * V2;
  137.  *   V += k * V2;
  138.  */
  139. #define VAddScaled(V, V1, k, V2)         \
  140.   { (V)[X] = (V1)[X] + (k) * (V2)[X];    \
  141.     (V)[Y] = (V1)[Y] + (k) * (V2)[Y];    \
  142.     (V)[Z] = (V1)[Z] + (k) * (V2)[Z]; }
  143.  
  144. #define VAddScaledEq(V, k, V2)  \
  145.   { (V)[X] += (k) * (V2)[X];    \
  146.     (V)[Y] += (k) * (V2)[Y];    \
  147.     (V)[Z] += (k) * (V2)[Z]; }
  148.  
  149.  
  150.  
  151. /*
  152.  * Subtract a scaled vector. [DB 8/94]
  153.  *
  154.  *   V  = V1 - k * V2;
  155.  *   V -= k * V2;
  156.  */
  157. #define VSubScaled(V, V1, k, V2)         \
  158.   { (V)[X] = (V1)[X] - (k) * (V2)[X];    \
  159.     (V)[Y] = (V1)[Y] - (k) * (V2)[Y];    \
  160.     (V)[Z] = (V1)[Z] - (k) * (V2)[Z]; }
  161.  
  162. #define VSubScaledEq(V, k, V2)  \
  163.   { (V)[X] -= (k) * (V2)[X];    \
  164.     (V)[Y] -= (k) * (V2)[Y];    \
  165.     (V)[Z] -= (k) * (V2)[Z]; }
  166.  
  167.  
  168.  
  169. /*
  170.  * Calculate the volume of a bounding box. [DB 8/94]
  171.  */
  172.  
  173. #define BOUNDS_VOLUME(a, b)                                   \
  174.   { (a) = (b).Lengths[X] * (b).Lengths[Y] * (b).Lengths[Z]; }
  175.  
  176.  
  177. /*
  178.  * Linear combination of 2 colours. [CEY]
  179.  *
  180.  *   C = k1 * C1 + k2 * C2
  181.  */
  182. #define CLinComb2(C, k1, C1, k2, C2)            \
  183.   { (C)[RED]    = (k1) * (C1)[RED]    + (k2) * (C2)[RED];   \
  184.     (C)[GREEN]  = (k1) * (C1)[GREEN]  + (k2) * (C2)[GREEN]; \
  185.     (C)[BLUE]   = (k1) * (C1)[BLUE]   + (k2) * (C2)[BLUE];  \
  186.     (C)[FILTER] = (k1) * (C1)[FILTER] + (k2) * (C2)[FILTER];\
  187.     (C)[TRANSM] = (k1) * (C1)[TRANSM] + (k2) * (C2)[TRANSM];}
  188.  
  189.  
  190. /* Misc. 4D Vector Math Macro Definitions */
  191. /* Inverse Scale - Divide Vector by a Scalar */
  192.  
  193. #define V4D_InverseScale(a, b, k) {(a)[X]=(b)[X]/(k);(a)[Y]=(b)[Y]/(k);(a)[Z]=(b)[Z]/(k);(a)[T]=(b)[T]/(k);}
  194. #define V4D_InverseScaleEq(a, k) {(a)[X]/=(k);(a)[Y]/=(k);(a)[Z]/=(k);(a)[T]/=(k);}
  195. /*
  196. #define V4D_InverseScale(a, b, k) {(a)[X]=(b)[X]*(1/k);(a)[Y]=(b)[Y]*(1/k);(a)[Z]=(b)[Z]*(1/k);(a)[T]=(b)[T]*(1/k);}
  197. #define V4D_InverseScaleEq(a, k) {(a)[X]*=(1/k);(a)[Y]*=(1/k);(a)[Z]*=(1/k);(a)[T]*=(1/k);}
  198. */
  199.  
  200. /* Dot Product - Gives Scalar angle (a) between two vectors (b) and (c) */
  201. #define V4D_Dot(a, b, c) {a=(b)[X]*(c)[X]+(b)[Y]*(c)[Y]+(b)[Z]*(c)[Z]+(b)[T]*(c)[T];}
  202.  
  203. #endif
  204.  
  205.  
  206.